<template>
  <XtxDialog
    :title="`${form.id ? '条件' : '修改'}收货地址`"
    v-model:visible="dialogVisible"
  >
    <div class="address-edit">
      <div class="xtx-form">
        <div class="xtx-form-item">
          <div class="label">收货人：</div>
          <div class="field">
            <input
              class="input"
              v-model="form.receiver"
              placeholder="请输入收货人"
            />
          </div>
        </div>
        <div class="xtx-form-item">
          <div class="label">手机号：</div>
          <div class="field">
            <input
              class="input"
              v-model="form.contact"
              placeholder="请输入手机号"
            />
          </div>
        </div>
        <div class="xtx-form-item">
          <div class="label">地区：</div>
          <div class="field">
            <xtxCity
              :fullLocation="form.fullLocation"
              @change="changeCity"
              placeholder="请选择所在地区"
            />
          </div>
        </div>
        <div class="xtx-form-item">
          <div class="label">详细地址：</div>
          <div class="field">
            <input
              class="input"
              v-model="form.address"
              placeholder="请输入详细地址"
            />
          </div>
        </div>
        <div class="xtx-form-item">
          <div class="label">邮政编码：</div>
          <div class="field">
            <input
              class="input"
              v-model="form.postalCode"
              placeholder="请输入邮政编码"
            />
          </div>
        </div>
        <div class="xtx-form-item">
          <div class="label">地址标签：</div>
          <div class="field">
            <input
              class="input"
              v-model="form.addressTags"
              placeholder="请输入地址标签，逗号分隔"
            />
          </div>
        </div>
      </div>
    </div>
    <template v-slot:footer>
      <XtxButton type="gray" style="margin-right: 20px" @click="closeDialog"
        >取消</XtxButton
      >
      <XtxButton type="primary" @click="submit">确认</XtxButton>
    </template>
  </XtxDialog>
</template>

<script>
import { reactive, ref } from 'vue'
import { addAddress, editAddress } from '@/api/order.js'
import Message from '@/components/library/Message'
export default {
  name: 'AddressEdit',
  setup(props, { emit }) {
    const dialogVisible = ref(false)
    const form = reactive({
      receiver: '',
      contact: '',
      provinceCode: '',
      cityCode: '',
      countyCode: '',
      address: '',
      postalCode: '',
      addressTags: '',
      isDefault: 1,
      // 完整的地址
      fullLocation: ''
    })
    // 打开函数
    const open = (address) => {
      dialogVisible.value = true
      if (address.id) {
        // 修改地址
        for (const key in address) {
          form[key] = address[key]
        }
      } else {
        // 添加地址
        form.receiver = ''
        form.contact = ''
        form.provinceCode = ''
        form.cityCode = ''
        form.countyCode = ''
        form.address = ''
        form.postalCode = ''
        form.fullLocation = ''
        form.addressTags = ''
        form.id = ''
      }
    }
    // 城市选中
    const changeCity = (result) => {
      // 整体地址
      form.fullLocation = result.fullLocation
      // 省份编码
      form.provinceCode = result.provinceCode
      // 城市编码
      form.cityCode = result.provinceCode
      // 地区编码
      form.countyCode = result.countyCode
    }
    // 关闭对话框
    const closeDialog = () => {
      dialogVisible.value = false
      form.receiver = ''
      form.contact = ''
      form.provinceCode = ''
      form.cityCode = ''
      form.countyCode = ''
      form.address = ''
      form.postalCode = ''
      form.fullLocation = ''
      form.addressTags = ''
    }
    // 提交数据
    const submit = () => {
      if (form.id) {
        // 修改请求
        editAddress(form).then((data) => {
          Message('success', '修改收获地址成功！')
          emit('on-success', form)
          dialogVisible.value = false
        })
      } else {
        // 添加组件
        addAddress(form).then((data) => {
          Message('success', '添加收获地址成功！')
          // 需要给结算组件地址列表中（加一条）
          // 出发自定义事件
          form.id = data.result.id
          emit('on-success', form)
          dialogVisible.value = false
        })
      }
    }
    return { dialogVisible, open, form, changeCity, closeDialog, submit }
  }
}
</script>
<style scoped lang="less">
.xtx-dialog {
  :deep(.wrapper) {
    width: 780px;
    .body {
      font-size: 14px;
    }
  }
}
.xtx-form {
  padding: 0;
  input {
    outline: none;
    &::placeholder {
      color: #ccc;
    }
  }
}
.xtx-city {
  width: 320px;
  :deep(.select) {
    height: 50px;
    line-height: 48px;
    display: flex;
    padding: 0 10px;
    justify-content: space-between;
    .placeholder {
      color: #ccc;
    }
    i {
      color: #ccc;
      font-size: 18px;
    }
    .value {
      font-size: 14px;
    }
  }
  :deep(.option) {
    top: 49px;
  }
}
</style>
